<?php
/**
 * 用户登录后的首页
 */
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    public function _initialize(){
        //判断用户是否登录
        $user = $_SESSION['user'];
        if(!$user){
            $this->error(L('_LOGIN_BEFORE'),U('Login/login'));
        }
        $first_nav = menu();
        foreach ($first_nav as $val){
            $w=array(
                'group_id'=>$user['group'],
                'menu_id' =>$val['id'],
                'status'  =>1
            );
            //权限表中有就显示
            $res=M('group_menu')->where($w)->find();
            if($res){
                //查询当前一级菜单下的二级菜单如果有有权限的二级菜单就显示一级菜单，如果没有有权限的二级菜单一级菜单就隐藏不显示
                $res_second_nav = M('menu')->where('pid='.$val['id'])->order('sort asc')->select();
                $menu_second = array();
                foreach ($res_second_nav as $val_second){
                    $where = array(
                        'status'=>1,
                        'group_id'=>$user['group'],
                        'menu_id'=>$val_second['id']
                    );
                    $r = M('group_menu')->where($where)->find();
                    if($r){
                        $val_second['nav_id']=$val_second['id'];
                        $menu_second[]=$val_second;
                    }else{
                        continue;
                    }
                }
                if($menu_second){
                    $val['action']=$menu_second[0]['action'];
                    $val['count']=count($menu_second);
                    $val['nav_id']=$menu_second[0]['id'];
                    $menu[$val['id']]=$val;
                    $menu[$val['id']]['second_nav']=$menu_second;
                }else{
                    continue;
                }
            }else{
                continue;
            }
        }
        $this->assign('menu',$menu);
    }

    public function _empty(){
        $this->redirect('Index/index');
    }

    public function index(){
        $config = M('sys_configs')->where('id = 1')->find();
        $this->assign('system_name',$config['system_name']);
        $this->display();
    }
    public function home(){
        $this->display();
    }

}
